/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: 北京银杉金服科技有限公司
 * 系统版本: 1.0.0
 * 开发人员: zhangfb
 * 开发时间: 2018/9/11 22:54
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.hyacinth.database.jpa;


import com.google.common.collect.Lists;
import com.hyacinth.constant.Constants;
import com.hyacinth.utils.DateUtils;
import com.hyacinth.utils.StringUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 条件查询封装类
 *
 * @author zhangfb
 * @version 1.0.0.1
 * @since JDK 1.8
 */
public final class SearchFilter {

    /** 字段名称 **/
    private final String fieldName;
    /** 操作符 **/
    private final Operator operator;
    /** 字段值 **/
    private final Object value;

    public SearchFilter(String fieldName, Operator operator, Object value) {
        this.fieldName = fieldName;
        this.value = value;
        this.operator = operator;
    }

    /**
     * 默认查询条件
     */
    public static List<SearchFilter> parseOk() {
        return Lists.newArrayList(new SearchFilter(Constants.dataFlag, Operator.EQ, Constants.dataFlag_OK));
    }

    /**
     * 将查询条件字符串转换为List<SearchFilter>
     *
     * @param conditionFilter 查询条件的json数组，
     *   格式为[{property:"字段名",option:"操作类型",value:"值",proptype:"数据类型"}]
     */
    public static List<SearchFilter> parse(String conditionFilter, boolean isOk) {
        if (StringUtils.isBlank(conditionFilter)) {
            return null;
        }
        //返回结果声明
        List<SearchFilter> filters = new ArrayList<>();
        JSONArray searchArray = JSONArray.fromObject(conditionFilter);
        for (int i = 0; i < searchArray.size(); i++) {
            JSONObject searchJsonObj = searchArray.getJSONObject(i);
            String property = searchJsonObj.getString("property");
            Object value = searchJsonObj.getString("value");
            if (property == null || value == null) {
                continue;
            }
            String option = searchJsonObj.getString("option");
            if (option == null) {
                option = Operator.EQ.name();
            }
            String fieldValue = (String) value;
            //判断是否是短日期格式的参数
            if ("DATETIME".equalsIgnoreCase(searchJsonObj.getString("proptype"))) {
                value = DateUtils.parse(fieldValue);
            } else if ("DATE".equalsIgnoreCase(searchJsonObj.getString("proptype"))) {
                if ("LTE".equalsIgnoreCase(searchJsonObj.getString("option"))) {
                    value = DateUtils.addDays(DateUtils.parse(fieldValue), 1);
                } else {
                    value = DateUtils.parse(fieldValue);
                }
            }
            //创建查询条件并将查询条件放入集合中
            SearchFilter filter = new SearchFilter(property, Operator.valueOf(option), value);
            filters.add(filter);
        }
        if (isOk) {
            filters.add(new SearchFilter(Constants.dataFlag, Operator.EQ, Constants.dataFlag_OK));
        }
        return filters;
    }

    public enum Operator {
        //EQ(等于), IN(在值数组中), ISNULL(非空), LIKE(包含), GT(大于), LT(小于),
        //GTE(大于等于), LTE(小于等于), NE(不等于),LIKEIGNORE(包含，忽略大小写)
        EQ, IN, ISNULL, LIKE, GT, LT, GTE, LTE, NE, LIKEIGNORE
    }

    public String getFieldName() {
        return fieldName;
    }

    public Operator getOperator() {
        return operator;
    }

    public Object getValue() {
        return value;
    }
}
